<?php
// +----------------------------------------------------------------------
// | Tplay [ WE ONLY DO WHAT IS NECESSARY ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017 http://tplay.pengyichen.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 听雨 < 389625819@qq.com >
// +----------------------------------------------------------------------


namespace app\admin\controller;

use app\admin\controller\User;
use app\admin\model\AdminMenu as menuModel;
use app\admin\model\Member as MemberModel;
use \think\Db;
use think\Validate;
use \think\Cookie;
use think\db\Query;
class Member extends User
{
    public function index()
    {
        $keywords=request()->param('keywords');
        if (isset($keywords) and !empty($keywords)) {
            $where['mname'] = ['like', '%' . $keywords . '%'];
        }
        $model = new MemberModel();
        $member = empty($where) ? $model->paginate(4,false,['query'=>request()->param()]) : $model->where($where)->paginate(4,false,['query'=>request()->param()]);

        foreach ($member as $v){
            $str=str_replace('&nbsp;','',$v['content']);
            $v['content']=strip_tags($str);
        }
        //halt($member);
        //$member =$model->all();
        $page=$member->render();


        return view('',['member'=>$member,'page'=>$page]);
    }
    public function publish()
    {
        //获取菜单id
        $id = $this->request->has('mid') ? $this->request->param('mid', 0, 'intval') : 0;
        $model = new MemberModel();
        //是正常添加操作
        if($id > 0) {
            //是修改操作
            if($this->request->isPost()) {
                //是提交操作
                $post = $this->request->post();
                //验证  唯一规则： 表名，字段名，排除主键值，主键名
                $validate=new Validate([
                    ['mname', 'require', '成员姓名不能为空'],
                    ['msex', 'require', '请选择成员性别'],
                    ['phone', 'require', '手机号不能为空'],
                    ['address', 'require', '城乡不能为空'],
                ]);
                //验证部分数据合法性
                if (!$validate->check($post)) {
                    $this->error('提交失败：' . $validate->getError());
                }
                //验证成员是否存在
                $cate = $model->where('mid',$id)->find();
                if(empty($cate)) {
                    return $this->error('id不正确');
                }
                if(false == $model->allowField(true)->save($post,['mid'=>$id])) {
                    return $this->error('修改失败');
                } else {
                    //addlog($operation_id=$id);//写入日志
                    return $this->success('修改成员成功','admin/member/index');
                }
            } else {
                //非提交操作
                $member = $model->where('mid',$id)->find();
                if(!empty($member)) {
                    $this->assign('member',$member);
                    return $this->fetch();
                } else {
                    return $this->error('id不正确');
                }
            }
        } else {
            //是新增操作
            if($this->request->isPost()) {
                //是提交操作
                $post = $this->request->post();
                $validate=new Validate([
                    ['mname', 'require', '成员姓名不能为空'],
                    ['msex', 'require', '请选择成员性别'],
                    ['phone', 'require', '手机号不能为空'],
                    ['address', 'require', '城乡不能为空'],
                ]);
                //验证  唯一规则： 表名，字段名，排除主键值，主键名
                //验证部分数据合法性
                //$post['create_time']=time();
                if (!$validate->check($post)) {
                    $this->error('提交失败：' . $validate->getError());
                }
                if(false == $model->allowField(true)->save($post)) {
                    return $this->error('添加失败');
                } else {
                    //addlog($id);//写入日志
                    return $this->success('添加成功','admin/member/index');
                }
            } else {
                //非提交操作
                $mid = $this->request->has('mid') ? $this->request->param('mid', null, 'intval') : null;
                if(!empty($mid)) {
                    $this->assign('mid',$mid);
                }
                return view('publish');
            }
        }

    }
    public function delete()
    {
        $model = new MemberModel();
        $id = $this->request->has('mid') ? $this->request->param('mid', 0, 'intval') : 0;
        //halt($id);
        if($id>0){
            if(false == $model->where('mid',$id)->delete()) {
                return $this->error('删除失败');
            } else {
                //addlog($id);//写入日志
                return $this->success('删除成功','admin/member/index');
            }
        }else{
            $this->error('id不存在');
        }
    }



}
